<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\adminapi\services\maintenance;

use app\dao\maintenance\LangDao;
use kitego\base\BaseService;
use think\db\exception\DbException;
use think\facade\Db;

class LangService extends BaseService
{
    public function __construct(LangDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 语言列表
     * @param int $page
     * @param int $pageSize
     * @param mixed $keyWords
     * @param int|null $status
     * @throws DbException
     */
    public function list(int $page, int $pageSize, mixed $keyWords = null, ?int $status)
    {
        $where[] = ['is_delete', '=', 0];
        if (isset($status)) {
            $where[] = ['status', '=', $status];
        }

        // 关键词模糊搜索
        $whereOr = [];
        if (isset($keyWords)) {
            $keyWords = trim($keyWords);
            $whereOr = [
                ['lang_name', 'like', '%' . $keyWords . '%'],
                ['lang_flag', 'like', '%' . $keyWords . '%']
            ];
        }

        return success($this->dao->page($where, $whereOr, '*', $page, $pageSize));
    }

    /**
     * 编辑语言
     * @param mixed $id 语言id
     * @param int $sort 排序值
     * @param string $langName 语言名称
     * @param string $langFlag 语言标示
     * @param string $status 开启状态
     * @param string $isDelete 是否删除
     * @param string $isDefault 是否默认
     */
    public function edit(
        mixed  $id, int $sort, string $langName, string $langFlag, string $status, string $isDelete,
        string $isDefault
    )
    {
        $data = [
            'sort' => $sort,
            'lang_name' => $langName,
            'lang_flag' => $langFlag,
            'status' => $status,
            'is_delete' => $isDelete,
            'is_default' => $isDefault,
        ];

        $res = Db::transaction(function () use ($id, $isDefault, $data) {
            if ($isDefault == 1) {
                $this->dao->update([['is_default', '=', 1]], ['is_default' => 0]);
            }

            if (!empty($id)) {
                $res = $this->dao->update($id, $data, 'id')->lang_name;
            } else {
                $res = $this->dao->save($data)->id;
            }

            return $res;
        });

        return success(['data' => $res]);
    }

    /**
     * 批量删除
     * @param array $ids
     * @throws DbException
     */
    public function batchDel(array $ids)
    {
        $this->dao->update([['id', 'in', $ids]], ['is_delete' => 1]);

        return success();
    }
}